package com.example.service;

import com.example.dao.ScoreDao;
import com.example.entity.Score;
import com.example.enums.ResultEnum;
import com.example.exception.StudentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author: John
 * @Date 2019/5/6 11:17
 */
@Service
public class ScoreService {

    private static final Logger logger = LoggerFactory.getLogger(ScoreService.class);

    @Resource
    ScoreDao scoreDao;

    @Resource
    private RedisTemplate redisTemplate;


    public Score searchBystudentId(String studentId){
        ValueOperations<String,Score> operations = redisTemplate.opsForValue();
        String key = "Score_"+studentId;
        Score score = operations.get(key);
        if(score!=null){
            //证明缓存中有score
            logger.info("ScoreService.searchBystudentId(): 从缓存中查询到成绩  >>"+score);
            return score;
        }else {
            Score score1 = scoreDao.searchBystudentId(studentId);
            if(score1 ==null){
                throw new StudentException(ResultEnum.SEARCH_EROOR);
            }else {
                operations.set(key,score1,10,TimeUnit.MINUTES);
                return score1;
            }
        }

    }


    public int insertScore(Score score){






        return scoreDao.insertScore(score);
    }
}
